package com.sky.controller.admin;

import com.sky.annotation.AutoFill;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;


    @ApiOperation("新增菜品")
    @PostMapping
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品{}", dishDTO);
        //清除redis中的缓存
        String key="dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);
        dishService.saveWithFlavor(dishDTO);
        return Result.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("分页查询{}", dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);


    }

    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result deleteDish(@RequestParam List<Long> ids) {
        log.info("删除菜品{}", ids);

        //清除redis中的缓存   --全部清除
         Set keys = redisTemplate.keys("dish_*");

         redisTemplate.delete(keys);

        dishService.delete(ids);

        return Result.success();

    }

    /**
     * 查询分类
     *
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品和口味")
    public Result<DishVO> getDishWithFlavorByid(@PathVariable Long id) {
        log.info("根据id查询菜品和口味{}", id);
        DishVO dishVO = dishService.getDishWithFlavorByid(id);
        return Result.success(dishVO);
    }

    @PutMapping
    @ApiOperation("修改菜品和对应的口味根据id")
    public Result updateDishWithFlavor(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品和对应口味:{}", dishDTO);
        //清除redis中的缓存   --全部清除
        Set keys = redisTemplate.keys("dish_*");

        redisTemplate.delete(keys);


        dishService.updateDishWithFlavor(dishDTO);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("根据菜品分类id查询菜品")
    public Result<List<Dish>> listDish(Long id) {
        log.info("根据菜品分类id查询菜品:{}", id);
        List<Dish> listDish = dishService.getDishByid(id);

        return Result.success(listDish);
    }

    /**
     * 启用、禁用分类
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用分类")
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id){

        //清除redis中的缓存   --全部清除
        Set keys = redisTemplate.keys("dish_*");

        redisTemplate.delete(keys);


        dishService.startOrStop(status,id);
        return Result.success();
    }

}
